package com.swak.fx.support;

import java.util.concurrent.TimeUnit;

import com.swak.config.setting.Settings;
import com.swak.fx.support.ui.UI.Conts;
import com.swak.reactivex.threads.Contexts;
import com.swak.reactivex.threads.OrderContext;
import com.swak.reactivex.threads.TimerContext;
import com.swak.reactivex.threads.WorkerContext;

import lombok.Getter;

/**
 * 系统 上下文
 * 
 * @author 618lf
 */
public class FxContexts {

	private static FxContexts Context = new FxContexts();

	public static FxContexts New() {
		return Context;
	}

	@Getter
	private final WorkerContext eventContext;
	@Getter
	private final OrderContext workerContext;
	@Getter
	private final TimerContext timerContext;
	private final Settings settings;

	FxContexts() {
		this.settings = Settings.intSettings();
		this.eventContext = Contexts.createWorkerContext("UI.eventbus-", getEventbusThreads(), true, 2,
				TimeUnit.SECONDS);
		this.timerContext = Contexts.createTimerContext("UI.timer-", true, 2, TimeUnit.SECONDS);
		this.workerContext = Contexts.createOrderContext("UI.worker-", getWorkerThreads(), true, 2, TimeUnit.SECONDS);
	}

	private int getEventbusThreads() {
		return Integer.parseInt(settings.getConfig(Conts.EventbusThreads_Key, "1"));
	}

	private int getWorkerThreads() {
		return Integer.parseInt(settings.getConfig(Conts.WorkerThreads_Key, "4"));
	}

	public void destory() {
		try {
			this.eventContext.shutdownNow();
		} catch (Exception e) {
		}
		try {
			this.workerContext.shutdownNow();
		} catch (Exception e) {
		}
		try {
			this.timerContext.stop();
		} catch (Exception e) {
		}
	}
}
